package com.store.service;

import com.store.dao.ExpresspositionMapper;
import com.store.model.ExpresspositionModel;
import com.store.model.OrderinfoModel;
import com.store.model.ResultData;
import com.store.model.StoreModel;
import com.store.utils.ConstantVariable;
import com.store.utils.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

@Service("expresspositionService")
public class ExpresspositionService {

    @Autowired
    private ExpresspositionMapper expresspositionMapper;

    @Autowired
    private ExcelUtil<ExpresspositionModel> excelUtil;

    public XSSFWorkbook export(ExpresspositionModel model) {
        model.setPageSize(Integer.MAX_VALUE);
        model.setPageNow(0);
        List<ExpresspositionModel> dataList = getList(model);
        return excelUtil.exportExcel(model, dataList);
    }

    public ResultData getData(ExpresspositionModel model) {
        ResultData returnData = new ResultData();
        try {
            returnData.setDataList(this.getList(model));
            int dataCount = this.getCount(model);
            returnData.setPageCount(dataCount, model.getPageSize());
            returnData.setDataCount(dataCount);
            returnData.setState(ConstantVariable.SUCCESS);
            returnData.setPageNumber(model.getPageNumber());
        } catch (Exception e) {
            returnData.setState(ConstantVariable.ERROR);
            returnData.setMsg(e.getMessage());
        }
        return returnData;
    }

    public ExpresspositionModel getModelById(ExpresspositionModel model) {
        List<ExpresspositionModel> dataList = getList(model);
        if (dataList.size() > 0) {
            return dataList.get(0);
        }
        return null;
    }

    public List<ExpresspositionModel> getList(ExpresspositionModel model) {
        return expresspositionMapper.getList(model);
    }

    public int getCount(ExpresspositionModel model) {
        return expresspositionMapper.getCount(model);
    }

    public ExpresspositionModel insert(ExpresspositionModel model) {
        expresspositionMapper.insert(model);
        return model;
    }

    public ExpresspositionModel update(ExpresspositionModel model) {
        expresspositionMapper.update(model);
        return model;
    }

    public List<ExpresspositionModel> batchUpdate(List<ExpresspositionModel> modelList) {
        for (ExpresspositionModel model : modelList) {
            expresspositionMapper.update(model);
        }
        return modelList;
    }

    public void disableOrEnable(ExpresspositionModel model) {
        expresspositionMapper.disableOrEnable(model);
    }

    public void delete(ExpresspositionModel model) {
        List<String> idList = new ArrayList<>();
        idList.add(model.getRecordid());
        model.setIdList(idList);
        expresspositionMapper.delete(model);
    }

    public String batchDelete(List<String> idList) {
        ExpresspositionModel model = new ExpresspositionModel();
        model.setIdList(idList);
        expresspositionMapper.delete(model);
        return ResultData.toSuccessString();
    }

    @Autowired
    private OrderinfoService orderinfoService;

    @Autowired
    private StoreService storeService;

    public Map<String, Object> getExpressPosition(String orderNo) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            OrderinfoModel orderinfoModel = new OrderinfoModel();
            orderinfoModel.setOrderno(orderNo);
            orderinfoModel = orderinfoService.getModelById(orderinfoModel);
            returnMap.put("orderinfo", orderinfoModel);
            String storeid = orderinfoModel.getStoreid();
            StoreModel storeModel = new StoreModel();
            storeModel.setStoreid(storeid);
            storeModel = storeService.getModelById(storeModel);
            returnMap.put("storeinfo", storeModel);
            ExpresspositionModel expresspositionModel = new ExpresspositionModel();
            expresspositionModel.setOrderno(orderNo);
            List<ExpresspositionModel> dataList = expresspositionMapper.getList(expresspositionModel);
            if (CollectionUtils.isNotEmpty(dataList)) {
                returnMap.put("positionlist", dataList);
                ExpresspositionModel model = new ExpresspositionModel(dataList.get(0));
                returnMap.put("currentposition", model);
            } else {
                returnMap.put("positionlist", "");
                returnMap.put("currentposition", new ArrayList<>());
            }
            return returnMap;
        } catch (Exception e) {
            e.printStackTrace();
            return returnMap;
        }
    }

}